package logic.utils;

import java.util.ArrayList;

import javax.swing.JFrame;

import ventanas.prueba.VentanaJuego;

import logic.blocks.Block;
import logic.characters.Link;
import logic.characters.Player;
import logic.enemies.Enemy;
import logic.entities.Background;
import logic.pruebas.GamePanel;

public class Game {
	private boolean end = false;

	public Game(Player player, int level, int difficulty, VentanaJuego window) {
		GamePanel panel = new GamePanel(300);
		panel.setLayout(null);
		window.add(panel);
		panel.setVisible(true);
//		String character = "Link";
		Background background = new Background(0, 0, panel, "resources/back" + level + ".gif", player);
//		window.setJugador(player);
		ArrayList<Block> blocks = new ArrayList<Block>();
		ArrayList<Enemy> enemies = new ArrayList<Enemy>();
		panel.setPlayer(player); 

		new MapLoader(blocks, enemies, panel, player, difficulty, level);

		background.dibujar();

		for (Block b : blocks) {
			b.dibujar();
		}		
		player.dibujar();
		panel.updateUI();
		player.enemies = enemies;
		player.blocks = blocks;		
		panel.timer.start();
		while (!end) {
			panel.drawTime();
			player.update();

			for (Block b : blocks) {
				b.update(background);
				b.impactoJugador(player);
				background.updateBackground(enemies);
				for (Enemy e : enemies) {
					e.update(player);
					b.impactoJugador(e);
					e.impactoJugador(player);
				}
			}
			panel.drawTime();
			panel.updateBars();
			panel.updateUI();
			if (player.dead || panel.time < 0 || player.endReached) {
				end = true;
			}
		}

	}
}

